﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Star_Wars
{
    class CollisionManager
    {
        private AsteroidManager asteroidManager;
        private PlayerManager playerManager;
        private ExplosionManager explosionManager;
        private GoalManager goalManager;
        private Vector2 offScreen = new Vector2(-500, -500);
        private Vector2 shotToAsteroidImpact = new Vector2(0, -20);
        //private int enemyPointValue = 100;

        public CollisionManager(AsteroidManager asteroidManager, PlayerManager playerManager, ExplosionManager explosionManager, GoalManager goalManager)
        {
            this.asteroidManager = asteroidManager;
            this.playerManager = playerManager;
            this.explosionManager = explosionManager;
            this.goalManager = goalManager;
        }
        private void checkAsteroidToPlayerCollisions()
        {
            if (!playerManager.Destroyed)
            {
                for (int i = 0; i < asteroidManager.Level * 4; i++)
                {
                    if (asteroidManager.Asteroids[i].IsCircleColliding(
                        playerManager.playerSprite.Center,
                        playerManager.playerSprite.CollisionRadius))
                    {
                        SoundManager.PlayExplosion();
                        explosionManager.AddExplosion(asteroidManager.Asteroids[i].Center, asteroidManager.Asteroids[i].Velocity / 10);
                        playerManager.Destroyed = true;
                        explosionManager.AddExplosion(playerManager.playerSprite.Center, Vector2.Zero);
                        asteroidManager.Asteroids[i].Location = offScreen;

                    }
                }
            }
        }

        private void checkPlayerToGoalCollisions()
        {
            if (!playerManager.Destroyed)
            {
                //goalManager.isAchieved = true;
                if (playerManager.playerSprite.IsCircleColliding(goalManager.goalSprite.Center, goalManager.goalSprite.CollisionRadius))
                {
                    goalManager.isAchieved = true;
                    playerManager.increasePlayerPoints();
                    playerManager.CountOfAchievedGoals++;
                    if (playerManager.CountOfAchievedGoals == 20)
                    {
                        if (asteroidManager.Level < 5) asteroidManager.Level++;
                        playerManager.CountOfAchievedGoals = 0;
                    }
                }
            }
        }

        public void CheckCollisions()
        {
            checkAsteroidToPlayerCollisions();
            checkPlayerToGoalCollisions();
        }
    }
}
